//
//  HYWelcomeViewController.swift
//  iOSWeibo
//
//  Created by why on 16/7/21.
//  Copyright © 2016年 王浩宇. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class HYWelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
        // Do any additional setup after loading the view.
        // 获取用户信息之前显示一次用户头像
        self.setIcon()
        
        //获取用户信息
        HYUserAccountViewModel.sharedUserAccountViewModel.loadUserInfo(){(error) -> () in
            if error != nil {
                print("获取用户信息失败")
                return
            }
            
            //获取信息成功了
            self.setIcon()
        }
    }
    
    ///设置头像
    private func setIcon() {
        if let avater_large = HYUserAccountViewModel.sharedUserAccountViewModel.userAccount?.avatar_large {
            let url = NSURL(string: avater_large)
            self.iconView.sd_setImageWithURL(url)
        }
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        startAniamtion()
    }
    
    private func startAniamtion() {
        let offset = -(UIScreen.mainScreen().bounds.height - 160)
        iconView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(self.view).offset(offset)
        }
        
        /// 带弹簧效果的动画
        // delay: 延迟时间
        // usingSpringWithDamping:弹簧明显效果 0 - 1
        // initialSpringVelocity: 初速度
        UIView.animateWithDuration(1, delay: 0.2, usingSpringWithDamping: 0.75, initialSpringVelocity: 7, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                // 动画完成
                print("动画完成")
                
                UIView.animateWithDuration(DefaultAnimtionDuration, animations: { () -> Void in
                    self.welcomeLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        // 切换到 mianTabBarVC
                        AppDelegate.switchRootViewController(HYMainTabBarController())
                        //                        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
                        //                        appDelegate.window?.rootViewController = HMMainTabBarController()
                })
        }
    }
    
    private func setupUI() {
        self.view.addSubview(bkgImageView)
        self.view.addSubview(iconView)
        self.view.addSubview(welcomeLabel)
        
        bkgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self.view)
        }
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.view)
            make.bottom.equalTo(self.view).offset(-160)
            make.size.equalTo(CGSize(width: 85, height: 85))
        }
        
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.view)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
    }
    
    ///MARK: - 懒加载控件
    ///背景
    private lazy var bkgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    ///头像
    private lazy var iconView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        imageView.layer.cornerRadius = 42.5
        imageView.layer.masksToBounds = true
        imageView.contentMode = UIViewContentMode.ScaleAspectFill
        
        return imageView
    }()
    
    ///欢迎label
    private lazy var welcomeLabel: UILabel = {
        let label = UILabel()
        
        label.text = "欢迎归来"
        label.font = UIFont.systemFontOfSize(18)
        
        label.sizeToFit()
        
        label.alpha = 0
        
        return label
    }()
}